package gobase

import (
	"fmt"
	"time"
)

/***
 * 如果时间为空，则返回""
 */
func DateTimeString2(val time.Time) string {
	if val.IsZero() {
		return ""
	} else {
		return val.Format("2006-01-02 15:04:05")
	}
}

func NowString() string {
	return time.Now().Format("2006-01-02 15:04:05")
}

func DateTimeFromatRFC3339(val time.Time) string {
	return val.Format(time.RFC3339)
}

// a:0时, f(a+6):6时, (a+23):23时,
func ToHourChr(atime time.Time) string {
	h := atime.Hour()
	i := int(rune('a'))
	rval := string(rune(i + h))
	return rval
}

// A:0时, F(A+6):6时, (A+23):23时,
func ToHourChrUpcase(atime time.Time) string {
	h := atime.Hour()
	i := int(rune('A'))
	rval := string(rune(i + h))
	return rval
}

func DayOfYear(atime time.Time) string {
	v := atime.YearDay()
	return fmt.Sprintf("%.3d", v)
}

const (
	timeRFC3339Len     = len(time.RFC3339)
	timeRFC3339NanoLen = len(time.RFC3339Nano)
)

func TryStrToDefTime(str string, def time.Time) time.Time {
	if len(str) == 0 {
		return def
	}

	strl := len(str)
	var rval time.Time
	var err error
	if strl == timeRFC3339Len {
		rval, err = time.Parse(time.RFC3339, str)
		if err == nil {
			return rval
		}
	} else if strl == timeRFC3339NanoLen {
		rval, err = time.Parse(time.RFC3339Nano, str)
		if err == nil {
			return rval
		}
	}

	location := def.Location()

	if strl == 19 {
		rval, err = time.ParseInLocation("2006-01-02 15:04:05", str, location)
		if err == nil {
			return rval
		}

		rval, err = time.ParseInLocation("2006/01/02 15:04:05", str, location)
		if err == nil {
			return rval
		}
		rval, err = time.ParseInLocation("2006-01-02/15:04:05", str, location)
		if err == nil {
			return rval
		}

		rval, err = time.ParseInLocation("2006 01 02 15 04 05", str, location)
		if err == nil {
			return rval
		}

	}

	rval, err = time.ParseInLocation("2006-01-02 15:04:05.999", str, location)
	if err == nil {
		return rval
	}

	rval, err = time.ParseInLocation("2006-01-02T15:04:05Z", str, location)
	if err == nil {
		return rval
	}

	rval, err = time.ParseInLocation("2006/1/2 15:4:5", str, location)
	if err == nil {
		return rval
	}
	rval, err = time.ParseInLocation("2006-1-2 15:4:5", str, location)
	if err == nil {
		return rval
	}

	rval, err = time.ParseInLocation("20060102150405", str, location)
	if err == nil {
		return rval
	}

	rval, err = time.ParseInLocation("2006/01/02", str, location)
	if err == nil {
		return rval
	}

	rval, err = time.ParseInLocation("2006/1/2", str, location)
	if err == nil {
		return rval
	}

	rval, err = time.ParseInLocation("2006-01-02", str, location)
	if err == nil {
		return rval
	}

	rval, err = time.ParseInLocation("2006-1-2", str, location)
	if err == nil {
		return rval
	}

	rval, err = time.ParseInLocation("20060102", str, location)
	if err == nil {
		return rval
	}

	rval, err = time.ParseInLocation("15:04:05", str, location)
	if err == nil {
		return rval
	}

	rval, err = time.ParseInLocation("15:04", str, location)
	if err == nil {
		return rval
	}

	return def
}

func GetDateTimeKeyValue(key string, _t time.Time) (v string, ok bool) {
	s1, s2 := Split2Str(key, ":")
	if len(s1) > 0 {
		if s1 == "u" || s1 == "utc" {
			_t = _t.UTC()
			key = s2
		}
	}
	if key == "yyyy" {
		return _t.Format("2006"), true
	} else if key == "yy" {
		return _t.Format("06"), true
	} else if key == "mm" {
		return _t.Format("01"), true
	} else if key == "dd" {
		return _t.Format("02"), true
	} else if key == "yyyymmdd" {
		return _t.Format("20060602"), true
	} else if key == "hh" {
		return _t.Format("15"), true
	} else if key == "mi" {
		return _t.Format("04"), true
	} else if key == "ss" {
		return _t.Format("05"), true
	} else if key == "doy" {
		return DayOfYear(_t), true
	} else if key == "HC" {
		return ToHourChrUpcase(_t), true
	} else if key == "hc" {
		return ToHourChr(_t), true
	} else if key == "qu" || key == "m15" {
		mi := _t.Minute()
		qua := mi / 15 * 15
		strQua := fmt.Sprintf("%.2d", qua)
		return strQua, true
	} else if key == "m5" {
		mi := _t.Minute()
		v0 := mi / 5 * 5
		str := fmt.Sprintf("%.2d", v0)
		return str, true
	} else if key == "m10" {
		mi := _t.Minute()
		v0 := mi / 10 * 10
		str := fmt.Sprintf("%.2d", v0)
		return str, true
	} else if key == "timeid" {
		return _t.Format("150405"), true
	}
	return "", false
}
